﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;

public partial class AddUser : System.Web.UI.Page
{
    private UserManageDAO userDAO = new UserManageDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
    }

    /// <summary>
    /// 添加用户按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitBtn_Click(object sender, EventArgs e)
    {
        if (IsUserExists())
        {
            Response.Write("<Script>alert('该用户已存在')</Script>");
        }
        else
        {
            Adduser();
            Response.Redirect(GlobalUser.LISTUSER_PAGE);
        }
        //Response.Write("");
    }

    /// <summary>
    /// 判定用户是否已存在
    /// </summary>
    /// <returns></returns>
    private Boolean IsUserExists()
    {
        Boolean flag = false;
        User temp = userDAO.GetUserByName(this.unameTextBox.Text.Trim());
        if (null != temp)
        {
            flag = true;
        }
        return flag;
    }

    /// <summary>
    /// 添加用户到数据库中
    /// </summary>
    /// <returns></returns>
    private Boolean Adduser()
    {
        Boolean flag = false;

        //生成要添加的用户对象
        User temp = new User();
        //用户名
        temp.name = this.unameTextBox.Text.Trim();
        //密码
        temp.password = this.upwdTextBox.Text.Trim();
        //绑定IP
        temp.ipLimit = this.IPTextBox.Text.Trim();
        //用户角色
        if (this.adminRadioButton.Checked)
        {
            temp.role = UserRole.EDITOR;
        }
        else
        {
            temp.role = UserRole.NORMAL;
        }

        //获取功能模块列表
        temp.grantedModulars = GetFunctionModulars();

        userDAO.AddUser(temp);
        return flag;
    }


    /// <summary>
    /// 获取选择的用户功能模块列表
    /// </summary>
    /// <returns></returns>
    public IList<FunctionModular> GetFunctionModulars()
    {
        IList<FunctionModular> result = new List<FunctionModular>();
        FunctionModular temp = null;

        //中文期刊功能
        if (this.zwqkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zhongwenqikan";
            result.Add(temp);
        }

        //外文期刊功能
        if(this.wwqkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "waiwenqikan";
            result.Add(temp);
        }

        //中文会议功能
        if (this.zwhyCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zhongwenhuiyi";
            result.Add(temp);
        }

        //外文会议
        if (this.wwhyCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "waiwenhuiyi";
            result.Add(temp);
        }

        //国电法规
         if (this.gdfgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guodianfagui";
            result.Add(temp);
        }

          //国家标准
         if (this.gjbzCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guojiabiaozhun";
            result.Add(temp);
        }

          //国际标准
         if (this.gjibzcheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guojibiaozhun";
            result.Add(temp);
        }
          //行业标准
         if (this.hybzCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "hangyebiaozhun";
            result.Add(temp);
        }
          //考察报告
         if (this.kcbgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kaochabaogao";
            result.Add(temp);
        }
          //国研报告
         if (this.gybgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guoyanbaogao";
            result.Add(temp);
        }
          //科技报告
         if (this.kjbgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kejibaogao";
            result.Add(temp);
        }
          //能源报告
         if (this.nybgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "nengyuanbaogao";
            result.Add(temp);
        }
          //学位论文
         if (this.xwlwCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "xueweilunwen";
            result.Add(temp);
        }

          //中国成果
         if (this.zgcgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zhongguochengguo";
            result.Add(temp);
        }
          //科技成果
         if (this.kjcgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kejichengguo";
            result.Add(temp);
        }
          //自产文献
         if (this.zcwxCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zichanwenxian";
            result.Add(temp);
        }
          //电力报刊
         if (this.dlbkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "dianlibaokan";
            result.Add(temp);
        }
          //科技报刊
         if (this.kjbkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kejibaokan";
            result.Add(temp);
        }

          //经济报刊
         if (this.jjbkcheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "jingjibaokan";
            result.Add(temp);
        }
          //综合报刊
         if (this.zhbkcheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zonghebaokan";
            result.Add(temp);
        }
          //图书手册
         if (this.tsscCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "tushushouce";
            result.Add(temp);
        }

        return result;
    }
}